package com.cn.aam.checaiduo.ui.useraccess;

import android.content.Context;
import android.content.Intent;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.cn.aam.checaiduo.Const;
import com.cn.aam.checaiduo.R;
import com.cn.aam.checaiduo.backend.RetrofitHome;
import com.cn.aam.checaiduo.backend.observer.OnRequestListener;
import com.cn.aam.checaiduo.backend.rx.RxSchedulers;
import com.cn.aam.checaiduo.base.ToolBarManager;
import com.cn.aam.checaiduo.bean.base.BaseResponseB;
import com.cn.aam.checaiduo.base.KeyBoardObserverActivity;
import com.cn.aam.checaiduo.utils.AuthUtil;
import com.cn.aam.checaiduo.widget.WMToast;
import com.cn.aam.checaiduo.utils.MyEditTextUtils;
import com.cn.aam.checaiduo.utils.MyViewsUtils;
import com.cn.aam.checaiduo.utils.StringUtil;
import com.orhanobut.logger.Logger;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 忘记密码操作
 */
public class ForgetPwdActivity extends KeyBoardObserverActivity {
    public static final int COUNT_DOWN = 120;//倒计时时间


    @BindView(R.id.iv_app_logo)
    ImageView ivAppLogo;

    @BindView(R.id.et_account)
    EditText etAccount;
    @BindView(R.id.et_code)
    EditText etCode;
    @BindView(R.id.et_pwd)
    EditText etPwd;

    @BindView(R.id.tv_send_code)
    TextView tvSendCode;


    private CountDownTimer cd;
    private boolean getCodeIng = false;//正在获取验证码中

    private String mobile;
    private String code;
    private String pwd;


    public static Intent newInstance(Context context) {
        Intent intent = new Intent(context, ForgetPwdActivity.class);
        return intent;
    }


    @Override
    protected int layoutId() {
        return R.layout.activity_forget_pwd;
    }


    @Override
    protected void initView() {
        ToolBarManager.with(this, WMContent)
                .setBackgroundColor(R.color.toolbar_bg_grey)
                .setTitle("忘记密码")
                .setTitleColor(R.color.black_normal)
                .setNavigationIcon(R.drawable.ic_nav_arrow_back);
    }

    private void getAllValue() {
        mobile = MyEditTextUtils.getContent(etAccount);
        code = MyEditTextUtils.getContent(etCode);
        pwd = MyEditTextUtils.getContent(etPwd);
    }

    //发送验证码
    @OnClick(R.id.tv_send_code)
    public void onSendCode() {
        if (checkMobileValue())
            doSendCode();

    }

    //忘记密码
    @OnClick(R.id.btn_forget)
    public void onSubmit() {
        if (checkAllValue())
            doForgetPwdStep2();

    }

    /**
     * 检测
     * 获取手机号码前
     */
    private boolean checkMobileValue() {
        getAllValue();
        if (TextUtils.isEmpty(mobile)) {
            WMToast.normal("请输入手机号码");
            return false;
        }
        if (!AuthUtil.isMobileNo(mobile)) {
            WMToast.normal("请输入正确的手机号码");
            return false;
        }
        return true;
    }


    /**
     * 检测
     * 提交前
     */
    private boolean checkAllValue() {
        getAllValue();
        if (!getCodeIng) {
            WMToast.normal("请获取验证码");
            return false;
        }
        if (!checkMobileValue())
            return false;
        if (TextUtils.isEmpty(mobile)) {
            WMToast.normal("请输入用户名");
            return false;
        }
        if (TextUtils.isEmpty(code)) {
            WMToast.normal("请输入验证码");
            return false;
        }
        if (TextUtils.isEmpty(pwd)) {
            WMToast.normal("请输入密码");
            return false;
        }
        if (!StringUtil.isValidateWith(pwd)) {
            WMToast.normal("请确保密码是6~16位字母与数字的组成");
            return false;
        }
        return true;
    }

    /**
     * 发送验证码
     */
    private void doSendCode() {
        RetrofitHome.getInstance()
                .getUserApi()
                .forgetPwdStep1(mobile)
                .compose(RxSchedulers.<BaseResponseB>toMain())
                .subscribe(new OnRequestListener<BaseResponseB>(this) {
                    @Override
                    public void onNext(BaseResponseB value) {
                        super.onNext(value);
                        if (value.getCode() == Const.REQUEST_SUCCESS)
                            startCountTime();
                    }
                });
    }


    /**
     * 忘记密码
     */
    private void doForgetPwdStep2() {
        RetrofitHome.getInstance()
                .getUserApi()
                .forgetPwdStep2(mobile, code, pwd)
                .compose(RxSchedulers.<BaseResponseB>toMain())
                .subscribe(new OnRequestListener<BaseResponseB>(this) {

                    @Override
                    protected String getProgressBarContent() {
                        return "注册中...";
                    }

                    @Override
                    public void onNext(BaseResponseB value) {
                        super.onNext(value);

                        WMToast.normal("修改密码成功");
                        onBackPressed();
                    }
                });

    }


    /**
     * 开始倒计时
     */
    private void startCountTime() {
        WMToast.normal("验证码已发送");
        getCodeIng = true;
        tvSendCode.setEnabled(false);
        //倒计时60s
        cd = new CountDownTimer(Const.MSG_COUNT_DOWN * 1000, 1000) {
            @Override
            public void onTick(long l) {
                tvSendCode.setText("重新发送(" + l / 1000 + "s)");
            }

            @Override
            public void onFinish() {
                endCountTime();
            }
        };
        cd.start();
    }

    /**
     * 结束倒计时
     */
    private void endCountTime() {
        if (cd != null) {
            cd.cancel();
            cd = null;
        }
        getCodeIng = false;
        tvSendCode.setText("获取验证码");
        tvSendCode.setEnabled(true);
    }


    @Override
    public void onBackPressed() {
        endCountTime();
        super.onBackPressed();
    }

    @Override
    protected void onKeyBoardShow() {
        if (ivAppLogo != null)
            MyViewsUtils.hideAllViews(ivAppLogo);
    }


    @Override
    protected void onKeyBoardDismiss() {
        if (ivAppLogo != null)
            MyViewsUtils.showAllViews(ivAppLogo);
    }

}
